package com.example.azheng.rxjavamvpdemo.ui.acitivity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.base.BaseActivity;
import com.example.azheng.rxjavamvpdemo.base.BaseMvpActivity;
import com.example.azheng.rxjavamvpdemo.bean.BaseObjectBean;
import com.example.azheng.rxjavamvpdemo.bean.code.CodeBean;
import com.example.azheng.rxjavamvpdemo.mvp.contract.RegisterContract;
import com.example.azheng.rxjavamvpdemo.mvp.presenter.RegisterPresenter;
import com.example.azheng.rxjavamvpdemo.util.ProgressDialog;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

public class RegistActivity extends BaseMvpActivity<RegisterPresenter> implements RegisterContract.View {
    @BindView(R.id.editText3)
    EditText edAccount;//账号
    @BindView(R.id.editText4)
    EditText edPassword;//密码
    @BindView(R.id.editText5)
    EditText edOkPassword;//确认密码
    @BindView(R.id.editText6)
    EditText edRealName;//真实姓名
    @BindView(R.id.editText7)
    EditText edPhone;//手机号
    @BindView(R.id.editText8)
    EditText edQQ;//qq号
    @BindView(R.id.editText9)
    EditText edWX;//微信号
    @BindView(R.id.editText10)
    EditText edCode;//验证码
    @BindView(R.id.iv_code)
    ImageView ivCode;//验证码图片
    @BindView(R.id.tv_register)
    TextView tvRegister;//立即注册
    @BindView(R.id.tv_login)
    TextView tvLogin;//前往登录
    private String key;
    @Override
    public int getLayoutId() {
        return R.layout.activity_regist;
    }
    @Override
    public void initView() {
        mPresenter = new RegisterPresenter();
        mPresenter.attachView(this);
        mPresenter.getCode();
    }

    @OnClick({R.id.tv_register,R.id.tv_login,R.id.iv_code})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tv_register:
                register();
                break;
            case R.id.tv_login:
                doActivity(LoginActivity.class);
                break;
            case R.id.iv_code:
                //点击刷新
                mPresenter.getCode();
                break;
        }
    }

    /**
     * 提交注册
     */
    private void register() {
        String account = edAccount.getText().toString();
        String password = edPassword.getText().toString();
        String oKPassword = edOkPassword.getText().toString();
        String realName =  edRealName.getText().toString();
        String phone  = edPhone.getText().toString();
        String qq = edQQ.getText().toString();
        String wx = edWX.getText().toString();
        String code = edCode.getText().toString();

        if(TextUtils.isEmpty(account)){
            showToast("请输入账号");
            return;
        }
        if(account.length() <4 || account.length()>15){
            showToast("请输入4-15位数字和字母");
            return;
        }
        if(TextUtils.isEmpty(password)){
            showToast("请输入密码");
            return;
        }
        if(TextUtils.isEmpty(oKPassword)){
            showToast("请输入确认密码");
            return;
        }

        if(!password.equals(oKPassword)){
            showToast("两次密码输入不一致");
            return;
        }
        if(password.length() <6 || password.length()>20){
            showToast("请输入6-20位数字、字符及特殊符号");
            return;
        }
        if(TextUtils.isEmpty(realName)){
            showToast("请输入真实姓名");
            return;
        }
        if(TextUtils.isEmpty(phone)){
            showToast("请输入手机号");
            return;
        }
        if(phone.length() != 11){
            showToast("请输入正确的手机号");
            return;
        }
        if(TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("account",account);
        map.put("pwd",password);
        map.put("username",realName);
        map.put("mobile",phone);
        map.put("qq",qq);
        map.put("wechat",wx);
        map.put("code",code);
        map.put("key",key);
        mPresenter.register(map);
    }

    @Override
    public void registerSuccess(BaseObjectBean<Object> bean) {
        showToast(bean.getMsg());
        if(bean.getCode() == 1){
            finish();
        }
    }

    @Override
    public void getCodeSuccess(BaseObjectBean<CodeBean> codeBean) {
        //加载glide 图片
        Glide.with(context).load(codeBean.getData().getPath()).into(ivCode);
        key = codeBean.getData().getKey();
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }
}
